package com.elink.enginerring.subject.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elink.enginerring.common.BaseCo;
import com.elink.enginerring.common.ErrorType;
import com.elink.enginerring.common.Pagination;
import com.elink.enginerring.subject.dao.CatalogMapper;
import com.elink.enginerring.subject.dao.SubjectBlogMapper;
import com.elink.enginerring.subject.dao.SubjectCourseMapper;
import com.elink.enginerring.subject.dao.SubjectFileMapper;
import com.elink.enginerring.subject.dao.SubjectMapper;
import com.elink.enginerring.subject.dao.SubjectNewsMapper;
import com.elink.enginerring.subject.dao.SubjectPostbarMapper;
import com.elink.enginerring.subject.entity.Catalog;
import com.elink.enginerring.subject.entity.Subject;
import com.elink.enginerring.subject.entity.SubjectBlog;
import com.elink.enginerring.subject.entity.SubjectCourse;
import com.elink.enginerring.subject.entity.SubjectFile;
import com.elink.enginerring.subject.entity.SubjectNews;
import com.elink.enginerring.subject.entity.SubjectPostbar;
import com.elink.enginerring.util.IDGenerator;
import com.elink.enginerring.util.JSONUtil;

@Service
public class SubjectService implements ISubjectService {
	@Autowired
	private SubjectMapper subjectMapper;
	@Autowired
	private SubjectFileMapper subjectFileMapper;
	@Autowired
	private SubjectNewsMapper subjectNewsMapper;
	@Autowired
	private SubjectBlogMapper subjectBlogMapper;
	@Autowired
	private SubjectCourseMapper subjectCourseMapper;
	@Autowired
	private SubjectPostbarMapper subjectPostbarMapper;
	@Autowired
	private CatalogMapper catalogMapper;

	@Override
	public Map<String, Object> getList(BaseCo co) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", subjectMapper.selectList(co));
		map.put("pagination", new Pagination(co, subjectMapper.selectCount(co)));
		return JSONUtil.getJSON(ErrorType.SUCCESS, map);
	}
	@Override
	public Map<String, Object> addSubject(Subject subject) {
		subject.setSubjectId(IDGenerator.gen32UUID());
		subjectMapper.insertSelective(subject);
		return  JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	@Override
	public Map<String, Object> addCatalogBySubjectId(Catalog cl) {
		cl.setCatalogId(IDGenerator.gen32UUID());
		catalogMapper.insertSelective(cl);
		return  JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	@Override
	public Map<String, Object> getCatalogListBySubjectId(String subjectId,BaseCo co) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", catalogMapper.selectListBySubjectId(subjectId,co));
		map.put("pagination", new Pagination(co, catalogMapper.selectCount(subjectId)));
		
		return JSONUtil.getJSON(ErrorType.SUCCESS, map);
	}

	@Override
	public Map<String, Object> getSubjectFileByCatalogId(String catalogId,int type, BaseCo co) {
		Map<String, Object> map = new HashMap<String, Object>();
	   if(type == 0){//资料
			map.put("list", subjectFileMapper.selectList(catalogId,co));
			map.put("pagination", new Pagination(co, subjectFileMapper.selectCount(catalogId,co)));
	   }else if(type == 1){//博文
			map.put("list", subjectBlogMapper.selectList(catalogId,co));
			map.put("pagination", new Pagination(co, subjectBlogMapper.selectCount(catalogId,co)));		   
	   }else if(type == 2){//课程
			map.put("list", subjectCourseMapper.selectList(catalogId,co));
			map.put("pagination", new Pagination(co, subjectCourseMapper.selectCount(catalogId,co)));		   
	   }else if(type == 3){//头条
			map.put("list", subjectNewsMapper.selectList(catalogId,co));
			map.put("pagination", new Pagination(co, subjectNewsMapper.selectCount(catalogId,co)));		   
	   }else if(type == 4){//百问百答
			map.put("list", subjectPostbarMapper.selectList(catalogId,co));
			map.put("pagination", new Pagination(co, subjectPostbarMapper.selectCount(catalogId,co)));		   
	   }
		return JSONUtil.getJSON(ErrorType.SUCCESS, map);
	}
	
	@Override
	public Map<String, Object> addSubjectFileByCatalogId(String catalogId,int type, BaseCo co) {
		Map<String, Object> map = new HashMap<String, Object>();
	   if(type == 0){//资料
			map.put("list", subjectFileMapper.selectListByCatalogId(catalogId,co));
			map.put("pagination", new Pagination(co, subjectFileMapper.selectCountByCatalogId(catalogId,co)));
	   }else if(type == 1){//博文
			map.put("list", subjectBlogMapper.selectListByCatalogId(catalogId,co));
			map.put("pagination", new Pagination(co, subjectBlogMapper.selectCountByCatalogId(catalogId,co)));		   
	   }else if(type == 2){//课程
			map.put("list", subjectCourseMapper.selectListByCatalogId(catalogId,co));
			map.put("pagination", new Pagination(co, subjectCourseMapper.selectCountByCatalogId(catalogId,co)));		   
	   }else if(type == 3){//头条
			map.put("list", subjectNewsMapper.selectListByCatalogId(catalogId,co));
			map.put("pagination", new Pagination(co, subjectNewsMapper.selectCountByCatalogId(catalogId,co)));		   
	   }else if(type == 4){//百问百答
			map.put("list", subjectPostbarMapper.selectListByCatalogId(catalogId,co));
			map.put("pagination", new Pagination(co, subjectPostbarMapper.selectCountByCatalogId(catalogId,co)));		   
	   }
		return JSONUtil.getJSON(ErrorType.SUCCESS, map);
	}
	@Override
	public Map<String, Object> addFile(String catalogId, String dcIds) {
		List<SubjectFile> fdc = new ArrayList<SubjectFile>();
		if(dcIds != null && dcIds != ""){
			for (String fileId : dcIds.split(",")) {
				SubjectFile sf = new SubjectFile();
				sf.setFileId(fileId);
				sf.setCatalogId(catalogId);
				sf.setSequence(0);
				fdc.add(sf);
			}
			subjectFileMapper.insertBatch(fdc);
		}
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	@Override
	public Map<String, Object> addNews(String catalogId, String dcIds) {
		List<SubjectNews> fdc = new ArrayList<SubjectNews>();
		if(dcIds != null && dcIds != ""){
			for (String fileId : dcIds.split(",")) {
				SubjectNews sn = new SubjectNews();
				sn.setNewsId(fileId);
				sn.setCatalogId(catalogId);
				sn.setSequence(0);
				fdc.add(sn);
			}
			subjectNewsMapper.insertBatch(fdc);
		}
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	@Override
	public Map<String, Object> addBlog(String catalogId, String dcIds) {
		List<SubjectBlog> fdc = new ArrayList<SubjectBlog>();
		if(dcIds != null && dcIds != ""){
			for (String fileId : dcIds.split(",")) {
				SubjectBlog sn = new SubjectBlog();
				sn.setBlogId(fileId);
				sn.setCatalogId(catalogId);
				sn.setSequence(0);
				fdc.add(sn);
			}
			subjectBlogMapper.insertBatch(fdc);
		}
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	@Override
	public Map<String, Object> addCourse(String catalogId, String dcIds) {
		List<SubjectCourse> fdc = new ArrayList<SubjectCourse>();
		if(dcIds != null && dcIds != ""){
			for (String fileId : dcIds.split(",")) {
				SubjectCourse sn = new SubjectCourse();
				sn.setCourseId(Integer.parseInt(fileId));
				sn.setCatalogId(catalogId);
				sn.setSequence(0);
				fdc.add(sn);
			}
			subjectCourseMapper.insertBatch(fdc);
		}
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	@Override
	public Map<String, Object> addPostbar(String catalogId, String dcIds) {
		List<SubjectPostbar> fdc = new ArrayList<SubjectPostbar>();
		if(dcIds != null && dcIds != ""){
			for (String fileId : dcIds.split(",")) {
				SubjectPostbar sn = new SubjectPostbar();
				sn.setPostbarId(fileId);
				sn.setCatalogId(catalogId);
				sn.setSequence(0);
				fdc.add(sn);
			}
			subjectPostbarMapper.insertBatch(fdc);
		}
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
}
